import Web3 from 'web3'
import contractAddress from '../../static/contract/contractAddress'
class Web3Sign {
  async subSign (data) {
    console.log(data, ';;;;;')
    let signHash = ''
    const ethereum = window.ethereum
    const web3 = new Web3(ethereum)
    ethereum.autoRefreshOnNetworkChange = true
    const accounts = await ethereum.request({
      method: 'eth_requestAccounts'
    })
    return new Promise(function(resolve) {
      console.log(accounts)
      // console.log(this.ImeetExchangeContract)
      const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
      const user = accounts[0].toLowerCase()
      const isOrderAsk = data.isOrderAsk
      const signer = user
      const collection = contractAddress.ImeetERC721Address.toLowerCase()
      const price = web3.utils.toWei(data.price, 'ether')
      const tokenId = data.tokenId
      const amount = 1
      const strategy = data.strategy.toLowerCase()
      const currency = contractAddress.ImeetERC20Address.toLowerCase()
      const nonce = data.tokenId
      // const startTime = Math.floor(Date.now() / 1000) - 100
      const startTime = data.startTime
      // const endTime = Math.floor(Date.now() / 1000) + 10000
      const endTime = data.endTime
      const minPercentageToAsk = 9000
      var Params = ''
      if (data.designatedAddressFlag) {
        Params = data.designatedAddress
      } else {
        Params = ZERO_ADDRESS
      }
      console.log(Params, 'Params')
      // var paramsHash = web3.utils.soliditySha3(Params)
      var paramsHash = Params
      // const MAKER_ORDER_HASH =
      //   '0x40261ade532fa1d2c7293df30aaadb9b3c616fae525a0b56d3d411c841a85028'.toLowerCase()
      const MAKER_ORDER_HASH =
        '0x88c456f996a2933340fc01fffd4cc870b889b2eb46c347c210f99821217797bf'.toLowerCase()
      console.log('paramsHash: ', paramsHash)
      var typeHash = web3.utils.soliditySha3(
        'EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'
      )
      console.log('typeHash: ', typeHash)
      console.log('MAKER_ORDER_HASH: ', MAKER_ORDER_HASH)
      console.log('isOrderAsk: ', isOrderAsk)
      console.log('signer: ', signer)
      console.log('collection: ', collection)
      console.log('price: ', price)
      console.log('tokenId: ', tokenId)
      console.log('strategy: ', strategy)
      console.log('currency: ', currency)
      console.log('nonce: ', nonce)
      console.log('startTime: ', startTime)
      console.log('endTime: ', endTime)
      console.log('minPercentageToAsk: ', minPercentageToAsk)
      console.log('paramsHash: ', paramsHash)
      var hashMsg = web3.utils.soliditySha3(
        { t: 'bytes32', v: MAKER_ORDER_HASH },
        { t: 'uint256', v: isOrderAsk },
        { t: 'uint256', v: signer },
        { t: 'uint256', v: collection },
        { t: 'uint256', v: price },
        { t: 'uint256', v: tokenId },
        { t: 'uint256', v: amount },
        { t: 'uint256', v: strategy },
        { t: 'uint256', v: currency },
        { t: 'uint256', v: nonce },
        { t: 'uint256', v: startTime },
        { t: 'uint256', v: endTime },
        { t: 'uint256', v: minPercentageToAsk },
        { t: 'uint256', v: paramsHash }
      )
      console.log('hashMsg:', hashMsg)
      const msgParams = JSON.stringify({
        domain: {
          chainId: 1337,
          name: 'ImeetNFT',
          // verifyingContract: '0x440623AF74020355c6da6bc32D55f6ED89c26390',
          verifyingContract: '0xd451dE245bf7C9666A32D07591F74BF0E6ad2B53',
          version: '1'
        },
        message: {
          orderhash: hashMsg
        },
        primaryType: 'OrderHash',
        types: {
          EIP712Domain: [
            { name: 'name', type: 'string' },
            { name: 'version', type: 'string' },
            { name: 'chainId', type: 'uint256' },
            { name: 'verifyingContract', type: 'address' }
          ],
          OrderHash: [
            {name: 'orderhash', type: 'bytes32'}
          ]
        }
      })
      var from = accounts[0]
      // 使用eth_signTypedData_V4结构化签名方法，这里只对最终哈希结构化
      var params = [from, msgParams]
      var method = 'eth_signTypedData_v4'
      // console.log(from, params, method, ';;;;;')
      web3.currentProvider.sendAsync(
        { method, params, from }, (err, result) => {
          // console.log(err, result, 'currentProvider')
          if (result) {
            // console.log(result.result, 'currentProvider, result')
            signHash = result.result
            // console.log(signHash)
            resolve(signHash)
            // signBack()
          } else {
            console.log(err, 'currentProvider,err')
            signHash = false
            console.log(signHash)
            // signBack()
            resolve(signHash)
          }
        }
      )
    })
  }
}
let web3Sign = new Web3Sign()
export default web3Sign
